home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr15 / neovgamp.zip / VGALOAD.C < prev    next >
Text File  |  1993-05-17  |  4KB  |  148 lines

  1. #include <graphics.h>
  2. #include <stdio.h>
  3. #include <alloc.h>
  4. #include <fcntl.h>
  5. #include <dos.h>
  6. #include <conio.h>
  7.  
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define MaxX 320
  11. #define MaxY 200
  12. #define MaxInt 32767
  13. #define ImageStart 772          /* Start of BitMap in Image File  */
  14. #define A_VGA_Driver 9
  15. #define PalOffset 4             /* Start of Palette in Image File */
  16.  
  17. int SetVideoMode(void);
  18. void SetPalette(char *InBuf);
  19.  
  20. /***********************************************************************
  21.  *                                                                     *
  22.  *   VGALoad first reads the image file from the command line and      *
  23.  *   determines if it is a valid bitmap.  It then reads and sets the   *
  24.  *   palette from the image file.  The buffer is filled and written    *
  25.  *   to video memory, which starts at [$A000:0000], until the file is  *
  26.  *   empty.  The image can be repostioned on the screen by adjusting   *
  27.  *   the MemIndex value.                                               *
  28.  *                                                                     *
  29.  ***********************************************************************/
  30.  
  31. void main(int argc,char *argv[])
  32.   {
  33.  
  34.     int Infile;
  35.     int Width;
  36.     int Height;
  37.     unsigned long BufSize;
  38.     long Vertical;
  39.     long Horizon;
  40.     long MemIndex;
  41.     long ArrayIndex;
  42.     unsigned char *InBuf;
  43.  
  44.  
  45.     if ((SetVideoMode() == TRUE) || (argc != 2))
  46.       {
  47.  
  48.     Infile = open(argv[1], O_RDONLY | O_BINARY);
  49.     BufSize=filelength( Infile );
  50.     if (BufSize > MaxInt)
  51.       BufSize = MaxInt;
  52.  
  53.     InBuf = malloc(BufSize);
  54.     read( Infile, InBuf, BufSize );
  55.  
  56.     Width    = *( InBuf + 0) + *( InBuf + 1)*256;
  57.     Height   = *( InBuf + 2) + *( InBuf + 3)*256;
  58.  
  59.     if ((Width < MaxX) && (Height < MaxY))
  60.       {
  61.  
  62.         SetPalette(InBuf);
  63.  
  64.         MemIndex = 0;
  65.         ArrayIndex = ImageStart;
  66.         for( Vertical = 1; Vertical <= Height; Vertical++)
  67.           for( Horizon = 1; Horizon <= MaxX; Horizon++)
  68.         {
  69.  
  70.           if (Horizon <= Width)
  71.             {
  72.             /* Place BitMap buffer into Video Memory */
  73.               poke( 0xA000, MemIndex, *( InBuf + ArrayIndex ));
  74.               if ( ArrayIndex >= BufSize)
  75.             {
  76.  
  77.               read( Infile, InBuf, BufSize );
  78.               ArrayIndex = 0;
  79.  
  80.             }
  81.               ArrayIndex++;
  82.             }
  83.           MemIndex++;
  84.         }
  85.         getch();
  86.       }
  87.  
  88.     free(InBuf);
  89.     close(Infile);
  90.  
  91.       }
  92.     textmode(LASTMODE);
  93.   }
  94.  
  95. /***********************************************************************
  96.  *                                                                     *
  97.  *   Sets video display to VGA 320x200x256 mode after first checking   *
  98.  *   to see if the monitor supports that mode.                         *
  99.  *                                                                     *
  100.  ***********************************************************************/
  101.  
  102. int SetVideoMode()
  103.   {
  104.  
  105.     union REGS regs;
  106.     int Driver;
  107.     int Mode;
  108.  
  109.     detectgraph( &Driver, &Mode );      /* Detects VGA Monitor */
  110.     if ( Driver == A_VGA_Driver )
  111.       {
  112.  
  113.     regs.h.ah = 0;
  114.     regs.h.al = 0x13;                /* 320x200x256 VGA Mode */
  115.     int86(0x10, ®s, ®s);
  116.     return(TRUE);
  117.  
  118.       }
  119.     else return(FALSE);
  120.  
  121.   }
  122.  
  123. /***********************************************************************
  124.  *                                                                     *
  125.  *   Sets the display palette to the current image palette before      *
  126.  *   displaying that image.  This C code uses port addresses to        *
  127.  *   change the video palette to the image palette, whereas the        *
  128.  *   Pascal Code uses interrupt $10.  Either one can be used with the  *
  129.  *   same results.                                                     *
  130.  *                                                                     *
  131.  ***********************************************************************/
  132.  
  133. void SetPalette(char *InBuf)
  134.   {
  135.     int color;
  136.  
  137.     for (color=0; color < 256; color++)
  138.       {
  139.  
  140.     outportb( 0x3C8, color );
  141.     outportb( 0x3C9, *( InBuf + PalOffset + (color * 3) + 0 ));
  142.     outportb( 0x3C9, *( InBuf + PalOffset + (color * 3) + 1 ));
  143.     outportb( 0x3C9, *( InBuf + PalOffset + (color * 3) + 2 ));
  144.  
  145.       }
  146.   }
  147.  
  148.